import os
import lzma

file = "1_lzma_压缩数据.py"
data = None
with open(file, "rb") as f:
    data = f.read()

# 压缩
with lzma.open(file + ".lzma", "wb") as f:
    f.write(data)
    # compress_data_len = f.peek    # NG, 返回的是压缩前的数据长度

# 解压
with lzma.open(file + ".lzma", "rb") as f:
    data_decompress = f.read()

compress_data_len = os.path.getsize(file + ".lzma")
print("压缩比:", compress_data_len / len(data))
print("lzma压缩数据是否一致: ", data == data_decompress)
